<template>
  <!-- 人员出勤填报表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="30%"
    draggable
    @close="closeDialog"
  >
    <el-descriptions
      class="margin-top"
      title=""
      :column="2"
      size="large"
      v-show="props.isReadonly"
      border
    >
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">填报日期</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.reportDate?.split(" ")[0] }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">班次</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.shiftName }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">班组</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.crewName }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">应出勤人数</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.planNumber }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">实际出勤人数</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.actualNumber }}
        </div>
      </el-descriptions-item>

    </el-descriptions>
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="110" v-show="!props.isReadonly">
      <el-row>
        <el-col :span="12">
          <el-form-item label="填报日期" prop="reportDate">
            <el-date-picker
              v-model="formData.reportDate"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="请选择填报日期"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="班次" prop="shiftName">
            <el-select
              v-model="formData.shiftName"
              placeholder="请选择班次"
              clearable
              style="width: 100%"
            >
              <el-option
                  v-for="item in shiftList"
                  :key="item.shiftId"
                  :label="item.shiftName"
                  :value="item.shiftName"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="班组" prop="crewName">
            <el-select
                v-model="formData.crewName"
                placeholder="请选择班组"
                clearable
                style="width: 100%"
            >
              <el-option
                v-for="item in useDictionary('AttendanceCrew')"
                :key="item.value"
                :label="item.label"
                :value="item.label"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="应出勤人数" prop="planNumber">
            <el-input v-model="formData.planNumber" placeholder="请输入应出勤人数" clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="实际出勤人数" prop="actualNumber">
            <el-input v-model="formData.actualNumber" placeholder="请输入实际出勤人数" clearable/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button v-if="!props.isReadonly" type="primary" :loading="loading" @click="handleSubmit">确 定</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import {useDictStoreHook} from "@/store/modules/dictionary";
import {useDictionary} from "@/utils/useTable";
import dayjs from "dayjs"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MESMESATTENDANCEREPORTADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  reportDate: [{required: true, message: "请选择填报日期", trigger: "blur"}],
  shiftId: [
    {required: true, message: "请选择班次", trigger: "change"}
  ],
  shiftName: [
    {required: true, message: "请选择班次", trigger: "change"}
  ],
  crewId: [
    {min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur"}
  ],
  crewName: [
    {required: true, message: "请选择班组", trigger: "change"}
  ],
  planNumber: [{required: true, message: "请输入应出勤人数", trigger: "blur"},
    { pattern: /^\d+$/, message: "请输入正整数", trigger: "blur"}
  ],
  actualNumber: [
    {required: true, message: "请输入实际出勤人数", trigger: "blur"},
    { pattern: /^\d+$/, message: "请输入正整数", trigger: "blur"},
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  reportDate: "",
  shiftId: "",
  shiftName: "",
  crewId: "",
  crewName: "",
  planNumber: "",
  actualNumber: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = ""
    if (newVal?.id) {
      const {
        id,
        reportDate,
        shiftId,
        shiftName,
        crewId,
        crewName,
        planNumber,
        actualNumber,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        reportDate,
        shiftId,
        shiftName,
        crewId,
        crewName,
        planNumber,
        actualNumber,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = ""
  formData.reportDate = ""
  formData.shiftId = ""
  formData.shiftName = ""
  formData.crewId = ""
  formData.crewName = ""
  formData.planNumber = ""
  formData.actualNumber = ""
  formData.version = ""
  formData.remarks = ""
  formData.createTime = ""
  formData.createdUserId = ""
  formData.createdUserName = ""
  formData.lastUpdateTime = ""
  formData.lastUpdatedUserId = ""
  formData.lastUpdatedUserName = ""
  formData.enabled = ""
  formData.deleted = ""
}

const loading = ref(false);

/** 人员出勤填报保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mes/mesattendancereport/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}


// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
const shiftList = ref([
  {
    shiftId: "A",
    shiftName: "A",
  },
  {
    shiftId: "B",
    shiftName: "B",
  },
]);

</script>
